#include "pagekeycbstat.h"
#include "ui_pagekeycbstat.h"
#include <QDebug>

PageKeyCBStat::PageKeyCBStat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageKeyCBStat)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    //setPageActive(false);

    if(ReadRstCirBreaksConfig_and_InitWidgets("./ResetCircuitBreaks.csv") == -1)
        qDebug()<<"read reset breaks error";
}

PageKeyCBStat::~PageKeyCBStat()
{
    delete ui;
}

void PageKeyCBStat::setPageActive(bool xActive)
{
    ui->ButtonReset->setEnabled(xActive);
}

void PageKeyCBStat::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

int PageKeyCBStat::ReadRstCirBreaksConfig_and_InitWidgets(QString path)
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        QWidget* cw = new QWidget(this);
        cw->setGeometry(10,100,1200,650);
        m_lWidgets.append(cw);
        cw->setVisible(i==0);
    }

    QFile file(path);
    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream stream(&file);
        QString strValue = stream.readLine();
        int labelsCnt[SaloonCnt] = {0};
        int allLabelCnt = 0;
        while(!stream.atEnd())
        {
            strValue = stream.readLine();
            if(strValue.isEmpty())
                continue;
            QStringList strList = strValue.split(",");
            RSTCircuits rc;
            rc.stat = 0;
            rc.ciruitName = strList.at(0).left(strList.at(0).size()-2) + "\n" + strList.at(1);
            rc.reset_id = strList.at(2).toInt()*10000 + strList.at(3).toInt()*10 + strList.at(4).toInt();
            rc.reset_cmd_id = strList.at(5).toInt()*10000 + strList.at(6).toInt()*10 + strList.at(7).toInt();
            rc.car = strList.at(0).right(1).toInt();
            QLabel *circuit_label = new QLabel(m_lWidgets[rc.car-1]);
            circuit_label->setStyleSheet("QLabel{background-color: rgb(0,255,0);font:14px 'Arial';color:black;}");
            circuit_label->setGeometry(120+(labelsCnt[rc.car-1]%4)*240,20+(labelsCnt[rc.car-1]/4)*60,230,50);
            circuit_label->setAlignment(Qt::AlignCenter);
            circuit_label->setText(rc.ciruitName);
            rc.pCiruitLabel = circuit_label;
            labelsCnt[rc.car-1]++;
            allLabelCnt++;
            m_lRSTCirBreaks.append(rc);
        }
    }
    else
        return -1;
    file.close();
    return 0;
}

void PageKeyCBStat::on_Btn_Mc1_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==0);
    }
}

void PageKeyCBStat::on_Btn_Mp1_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==1);
    }
}

void PageKeyCBStat::on_Btn_M1_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==2);
    }
}

void PageKeyCBStat::on_Btn_M2_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==3);
    }
}

void PageKeyCBStat::on_Btn_Mp2_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==4);
    }
}

void PageKeyCBStat::on_Btn_Tc2_clicked()
{
    for (int i=0;i<SaloonCnt ;i++ ) {
        m_lWidgets.at(i)->setVisible(i==5);
    }
}

void PageKeyCBStat::on_ButtonReset_clicked()
{
    emit signal_ResetCircuitBreak(true);
    ui->ButtonReset->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_ResetCircuitBreak(false);
        ui->ButtonReset->setEnabled(true);
    });
}

void PageKeyCBStat::setCiruitBreakStat(int id, int stat)
{
    QString labelstyle = "QLabel{background-color: rgb(0,255,0);font:14px 'Arial';color:black;}";
    if(stat)
      labelstyle = "QLabel{background-color: rgb(255,0,0);font:14px 'Arial';color:black;}";
    for(int i=0;i<m_lRSTCirBreaks.length();i++)
    {
        if(m_lRSTCirBreaks.at(i).reset_id == id){
            m_lRSTCirBreaks[i].stat = stat;
            m_lRSTCirBreaks[i].pCiruitLabel->setStyleSheet(labelstyle);
            break;
        }
    }
}
